#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>

void Swap(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}
void AdjustDown(int* a, int n, int parent)
{
	int child = parent * 2 + 1;
	while (child < n)
	{
		if (child + 1 < n && a[child] < a[child + 1])
		{
			child++;
		}

		if (a[child] > a[parent])
		{
			Swap(&a[child], &a[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}

void HeapSort(int* a, int n)
{
	int i = 0;
	for (i = (n - 2) / 2; i >= 0; i--)
	{
		AdjustDown(a, n, i);
	}
	i = 1;
	while(i<n)
	{
		Swap(&a[0], &a[n - i]);
		AdjustDown(a, n - i, 0);

		i++;
	}
	


}
int main()
{
	int arr[] = { 19,15,23,45,78,14,0,1,6 };
	int n = sizeof(arr) / sizeof(int);
	HeapSort(arr,n);


	return 0;
	
}